<?php
session_start();
require_once 'config.php';
require_once 'functions.php';

// 检查IP是否被封禁
$user_ip = get_user_ip();
if (is_ip_banned($user_ip)) {
    die("<div style='text-align: center; margin-top: 50px;'><h1>您的IP已被封禁，无法访问本站</h1></div>");
}

// 确定当前显示的模块
$active_module = isset($_GET['module']) ? $_GET['module'] : 'confession';
if (!in_array($active_module, ['confession', 'daily', 'announcement'])) {
    $active_module = 'confession';
}

// 获取音乐URL
$music_url = get_setting('music_url');

// 获取头部图片
$header_image = get_setting('header_image', 'images/header.jpg');
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="./static/css/bootstrap.min.css">
    <link rel="stylesheet" href="./static/css/all.min.css">
    <style>
        /* 全局样式 */
        body {
            background-color: #fff5f8;
            color: #333;
            font-family: 'Arial', sans-serif;
            overflow-x: hidden;
        }
        
        /* 樱花飘落效果 */
        .sakura {
            position: fixed;
            background-color: #ffccd5;
            border-radius: 50% 50% 30% 70%;
            pointer-events: none;
            z-index: 100;
            animation: fall linear infinite;
        }
        
        @keyframes fall {
            to {
                transform: translateY(100vh) rotate(720deg);
                opacity: 0;
            }
        }
        
        /* 头部样式 */
        .site-header {
            background-color: #ff8fab;
            padding: 20px 0;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .site-title {
            color: white;
            text-align: center;
            font-size: 2.5rem;
            margin: 0;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
        }
        
        .header-image {
            width: 100%;
            max-height: 300px;
            object-fit: cover;
            margin: 20px 0;
            border-radius: 10px;
            box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        }
        
        /* 导航模块样式 */
        .module-nav {
            display: flex;
            justify-content: center;
            margin: 20px 0;
            flex-wrap: wrap;
        }
        
        .module-btn {
            background-color: #ffb6c1;
            color: white;
            border: none;
            padding: 10px 25px;
            margin: 0 10px 10px;
            border-radius: 30px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .module-btn.active {
            background-color: #ff69b4;
            transform: scale(1.05);
        }
        
        .module-btn:hover:not(.active) {
            background-color: #ff9aa2;
        }
        
        /* 内容区域样式 */
        .content-container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 0 15px;
        }
        
        .module-content {
            display: none;
            background-color: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            margin-bottom: 30px;
        }
        
        .module-content.active {
            display: block;
        }
        
        /* 发布按钮 */
        .post-btn {
            position: fixed;
            right: 30px;
            bottom: 30px;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background-color: #ff69b4;
            color: white;
            border: none;
            font-size: 1.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 10px rgba(0,0,0,0.2);
            cursor: pointer;
            transition: all 0.3s ease;
            z-index: 90;
        }
        
        .post-btn:hover {
            transform: scale(1.1);
            background-color: #ff4757;
        }
        
        /* 卡片样式 */
        .post-card {
            background-color: #fff0f3;
            border-radius: 12px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            transition: transform 0.3s ease;
        }
        
        .post-card:hover {
            transform: translateY(-5px);
        }
        
        .post-card.top {
            border: 2px solid #ff69b4;
        }
        
        .post-header {
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 1px dashed #ffccd5;
        }
        
        .post-title {
            font-size: 1.2rem;
            color: #d63384;
            margin: 0 0 5px 0;
        }
        
        .post-meta {
            font-size: 0.9rem;
            color: #888;
        }
        
        .post-image {
            max-width: 100%;
            border-radius: 8px;
            margin: 15px 0;
        }
        
        .post-content {
            line-height: 1.6;
            margin-bottom: 15px;
        }
        
        .post-actions {
            display: flex;
            gap: 15px;
            margin-top: 10px;
        }
        
        .action-btn {
            background: none;
            border: none;
            color: #ff69b4;
            display: flex;
            align-items: center;
            gap: 5px;
            cursor: pointer;
            transition: color 0.2s;
        }
        
        .action-btn:hover {
            color: #d63384;
        }
        
        .action-btn.liked {
            color: #e91e63;
        }
        
        /* 评论区样式 */
        .comments-section {
            margin-top: 20px;
            padding-top: 15px;
            border-top: 1px dashed #ffccd5;
        }
        
        .comment-card {
            background-color: white;
            border-radius: 8px;
            padding: 12px;
            margin-bottom: 10px;
            font-size: 0.95rem;
        }
        
        .comment-form {
            margin-top: 15px;
        }
        
        .comment-input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ffccd5;
            border-radius: 20px;
            margin-bottom: 10px;
            resize: none;
        }
        
        .comment-submit {
            background-color: #ff69b4;
            color: white;
            border: none;
            padding: 6px 15px;
            border-radius: 20px;
            cursor: pointer;
        }
        
        /* 底部备案信息 */
        .site-footer {
            background-color: #ff8fab;
            color: white;
            text-align: center;
            padding: 20px 0;
            margin-top: 50px;
        }
        
        .icp-link {
            color: white;
            text-decoration: underline;
        }
        
        .icp-link:hover {
            color: #fff0f3;
        }
        
        /* 响应式调整 */
        @media (max-width: 768px) {
            .site-title {
                font-size: 2rem;
            }
            
            .module-btn {
                padding: 8px 20px;
                font-size: 1rem;
            }
            
            .post-btn {
                width: 50px;
                height: 50px;
                font-size: 1.2rem;
            }
        }
    </style>
</head>
<body>
    <!-- 樱花飘落效果容器 -->
    <div id="sakura-container"></div>
    
    <!-- 音乐播放器 -->
    <?php if (!empty($music_url)): ?>
    <audio id="background-music" loop>
        <source src="<?php echo $music_url; ?>" type="audio/mpeg">
    </audio>
    <?php endif; ?>
    
    <!-- 头部 -->
    <header class="site-header">
        <div class="container">
            <h1 class="site-title"><?php echo SITE_NAME; ?></h1>
        </div>
    </header>
    
    <div class="container content-container">
        <!-- 头部图片 -->
        <img src="<?php echo $header_image; ?>" alt="网站头部图片" class="header-image">
        
        <!-- 模块导航 -->
        <div class="module-nav">
            <button class="module-btn <?php echo $active_module == 'confession' ? 'active' : ''; ?>" onclick="window.location='?module=confession'">
                <i class="fas fa-heart"></i> 表白
            </button>
            <button class="module-btn <?php echo $active_module == 'daily' ? 'active' : ''; ?>" onclick="window.location='?module=daily'">
                <i class="fas fa-coffee"></i> 日常
            </button>
            <button class="module-btn <?php echo $active_module == 'announcement' ? 'active' : ''; ?>" onclick="window.location='?module=announcement'">
                <i class="fas fa-bullhorn"></i> 公告
            </button>
        </div>
        
        <!-- 表白模块 -->
        <div class="module-content <?php echo $active_module == 'confession' ? 'active' : ''; ?>" id="confession-module">
            <h2 style="color: #ff69b4; text-align: center; margin-bottom: 20px;"><i class="fas fa-heart"></i> 表白墙</h2>
            
            <?php
            // 获取表白内容，优先显示置顶的
            $stmt = $pdo->query("SELECT * FROM confessions ORDER BY is_top DESC, created_at DESC");
            $confessions = $stmt->fetchAll();
            
            if (count($confessions) > 0) {
                foreach ($confessions as $confession) {
            ?>
                <div class="post-card <?php echo $confession['is_top'] ? 'top' : ''; ?>">
                    <?php if ($confession['is_top']): ?>
                        <div style="background-color: #ff69b4; color: white; display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 0.8rem; margin-bottom: 10px;">
                            <i class="fas fa-thumbtack"></i> 置顶
                        </div>
                    <?php endif; ?>
                    
                    <div class="post-header">
                        <h3 class="post-title">
                            <?php echo $confession['is_anonymous'] ? '匿名用户' : htmlspecialchars($confession['confessor']); ?>
                            向 <?php echo htmlspecialchars($confession['target_person']); ?> 表白
                        </h3>
                        <div class="post-meta">
                            <span>ID: <?php echo $confession['id']; ?></span> · 
                            <span><?php echo date('Y-m-d H:i', strtotime($confession['created_at'])); ?></span>
                        </div>
                    </div>
                    
                    <div class="post-content">
                        <?php echo nl2br(htmlspecialchars($confession['content'])); ?>
                    </div>
                    
                    <?php if (!empty($confession['image_url'])): ?>
                        <img src="<?php echo UPLOAD_DIR . $confession['image_url']; ?>" alt="表白图片" class="post-image">
                    <?php endif; ?>
                    
                    <div class="post-actions">
                        <button class="action-btn like-btn" data-type="confession" data-id="<?php echo $confession['id']; ?>">
                            <i class="fas fa-heart<?php echo has_liked('confession', $confession['id'], $user_ip) ? ' liked' : ''; ?>"></i>
                            <span class="like-count"><?php echo get_like_count('confession', $confession['id']); ?></span>
                        </button>
                        <button class="action-btn comment-toggle" data-type="confession" data-id="<?php echo $confession['id']; ?>">
                            <i class="fas fa-comment"></i>
                            <span><?php echo get_comment_count('confession', $confession['id']); ?></span>
                        </button>
                    </div>
                    
                    <!-- 评论区 -->
                    <div class="comments-section" id="comments-confession-<?php echo $confession['id']; ?>" style="display: none;">
                        <?php
                        // 获取评论
                        $stmt = $pdo->prepare("SELECT * FROM comments WHERE type = 'confession' AND target_id = ? ORDER BY created_at ASC");
                        $stmt->execute([$confession['id']]);
                        $comments = $stmt->fetchAll();
                        
                        if (count($comments) > 0) {
                            foreach ($comments as $comment) {
                        ?>
                                <div class="comment-card">
                                    <div class="post-meta" style="margin-bottom: 5px;">
                                        <span><?php echo date('Y-m-d H:i', strtotime($comment['created_at'])); ?></span>
                                    </div>
                                    <div><?php echo nl2br(htmlspecialchars($comment['content'])); ?></div>
                                </div>
                        <?php
                            }
                        } else {
                            echo '<p style="text-align: center; color: #888;">还没有评论，快来抢沙发吧~</p>';
                        }
                        ?>
                        
                        <!-- 评论表单 -->
                        <div class="comment-form">
                            <textarea class="comment-input" rows="2" placeholder="写下你的评论..."></textarea>
                            <button class="comment-submit" data-type="confession" data-id="<?php echo $confession['id']; ?>">发送</button>
                        </div>
                    </div>
                </div>
            <?php
                }
            } else {
                echo '<div style="text-align: center; padding: 50px 0; color: #888;">
                        <i class="fas fa-heart" style="font-size: 3rem; margin-bottom: 20px; color: #ffccd5;"></i>
                        <p>还没有人发表表白哦，快来成为第一个吧~</p>
                      </div>';
            }
            ?>
        </div>
        
        <!-- 日常模块 -->
        <div class="module-content <?php echo $active_module == 'daily' ? 'active' : ''; ?>" id="daily-module">
            <h2 style="color: #ff69b4; text-align: center; margin-bottom: 20px;"><i class="fas fa-coffee"></i> 日常分享</h2>
            
            <?php
            // 获取日常内容，优先显示置顶的
            $stmt = $pdo->query("SELECT * FROM dailies ORDER BY is_top DESC, created_at DESC");
            $dailies = $stmt->fetchAll();
            
            if (count($dailies) > 0) {
                foreach ($dailies as $daily) {
            ?>
                <div class="post-card <?php echo $daily['is_top'] ? 'top' : ''; ?>">
                    <?php if ($daily['is_top']): ?>
                        <div style="background-color: #ff69b4; color: white; display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 0.8rem; margin-bottom: 10px;">
                            <i class="fas fa-thumbtack"></i> 置顶
                        </div>
                    <?php endif; ?>
                    
                    <div class="post-header">
                        <h3 class="post-title"><?php echo htmlspecialchars($daily['poster']); ?> 的日常</h3>
                        <div class="post-meta">
                            <span>ID: <?php echo $daily['id']; ?></span> · 
                            <span><?php echo date('Y-m-d H:i', strtotime($daily['created_at'])); ?></span>
                        </div>
                    </div>
                    
                    <div class="post-content">
                        <?php echo nl2br(htmlspecialchars($daily['content'])); ?>
                    </div>
                    
                    <?php if (!empty($daily['image_url'])): ?>
                        <img src="<?php echo UPLOAD_DIR . $daily['image_url']; ?>" alt="日常图片" class="post-image">
                    <?php endif; ?>
                    
                    <div class="post-actions">
                        <button class="action-btn like-btn" data-type="daily" data-id="<?php echo $daily['id']; ?>">
                            <i class="fas fa-heart<?php echo has_liked('daily', $daily['id'], $user_ip) ? ' liked' : ''; ?>"></i>
                            <span class="like-count"><?php echo get_like_count('daily', $daily['id']); ?></span>
                        </button>
                        <button class="action-btn comment-toggle" data-type="daily" data-id="<?php echo $daily['id']; ?>">
                            <i class="fas fa-comment"></i>
                            <span><?php echo get_comment_count('daily', $daily['id']); ?></span>
                        </button>
                    </div>
                    
                    <!-- 评论区 -->
                    <div class="comments-section" id="comments-daily-<?php echo $daily['id']; ?>" style="display: none;">
                        <?php
                        // 获取评论
                        $stmt = $pdo->prepare("SELECT * FROM comments WHERE type = 'daily' AND target_id = ? ORDER BY created_at ASC");
                        $stmt->execute([$daily['id']]);
                        $comments = $stmt->fetchAll();
                        
                        if (count($comments) > 0) {
                            foreach ($comments as $comment) {
                        ?>
                                <div class="comment-card">
                                    <div class="post-meta" style="margin-bottom: 5px;">
                                        <span><?php echo date('Y-m-d H:i', strtotime($comment['created_at'])); ?></span>
                                    </div>
                                    <div><?php echo nl2br(htmlspecialchars($comment['content'])); ?></div>
                                </div>
                        <?php
                            }
                        } else {
                            echo '<p style="text-align: center; color: #888;">还没有评论，快来抢沙发吧~</p>';
                        }
                        ?>
                        
                        <!-- 评论表单 -->
                        <div class="comment-form">
                            <textarea class="comment-input" rows="2" placeholder="写下你的评论..."></textarea>
                            <button class="comment-submit" data-type="daily" data-id="<?php echo $daily['id']; ?>">发送</button>
                        </div>
                    </div>
                </div>
            <?php
                }
            } else {
                echo '<div style="text-align: center; padding: 50px 0; color: #888;">
                        <i class="fas fa-coffee" style="font-size: 3rem; margin-bottom: 20px; color: #ffccd5;"></i>
                        <p>还没有人分享日常哦，快来成为第一个吧~</p>
                      </div>';
            }
            ?>
        </div>
        
        <!-- 公告模块 -->
        <div class="module-content <?php echo $active_module == 'announcement' ? 'active' : ''; ?>" id="announcement-module">
            <h2 style="color: #ff69b4; text-align: center; margin-bottom: 20px;"><i class="fas fa-bullhorn"></i> 公告通知</h2>
            
            <?php
            // 获取公告内容，优先显示置顶的
            $stmt = $pdo->query("SELECT * FROM announcements ORDER BY is_top DESC, created_at DESC");
            $announcements = $stmt->fetchAll();
            
            if (count($announcements) > 0) {
                foreach ($announcements as $announcement) {
            ?>
                <div class="post-card <?php echo $announcement['is_top'] ? 'top' : ''; ?>">
                    <?php if ($announcement['is_top']): ?>
                        <div style="background-color: #ff69b4; color: white; display: inline-block; padding: 2px 10px; border-radius: 10px; font-size: 0.8rem; margin-bottom: 10px;">
                            <i class="fas fa-thumbtack"></i> 置顶公告
                        </div>
                    <?php endif; ?>
                    
                    <div class="post-header">
                        <h3 class="post-title"><?php echo htmlspecialchars($announcement['title']); ?></h3>
                        <div class="post-meta">
                            <span>ID: <?php echo $announcement['id']; ?></span> · 
                            <span><?php echo date('Y-m-d H:i', strtotime($announcement['created_at'])); ?></span>
                        </div>
                    </div>
                    
                    <div class="post-content">
                        <?php echo nl2br(htmlspecialchars($announcement['content'])); ?>
                    </div>
                </div>
            <?php
                }
            } else {
                echo '<div style="text-align: center; padding: 50px 0; color: #888;">
                        <i class="fas fa-bullhorn" style="font-size: 3rem; margin-bottom: 20px; color: #ffccd5;"></i>
                        <p>暂无公告发布</p>
                      </div>';
            }
            ?>
        </div>
    </div>
    
    <!-- 发布按钮 -->
    <button class="post-btn" onclick="window.location='post.php?type=<?php echo $active_module; ?>'">
        <i class="fas fa-plus"></i>
    </button>
    
    <!-- 底部 -->
    <footer class="site-footer">
        <div class="container">
            <p>© <?php echo date('Y'); ?> <?php echo SITE_NAME; ?> - 校园表白墙</p>
            <p>
                <a href="<?php echo ICP_URL; ?>" target="_blank" class="icp-link">
                    <?php echo ICP_RECORD; ?>
                </a>
            </p>
        </div>
    </footer>
    
    <script src="./static/js/jquery-3.6.0.min.js"></script>
    <script>
        // 樱花飘落效果
        function createSakura() {
            const sakuraContainer = document.getElementById('sakura-container');
            const sakura = document.createElement('div');
            sakura.classList.add('sakura');
            
            // 随机大小
            const size = Math.random() * 10 + 5;
            sakura.style.width = `${size}px`;
            sakura.style.height = `${size}px`;
            
            // 随机位置
            sakura.style.left = `${Math.random() * 100}vw`;
            sakura.style.top = `-${size}px`;
            
            // 随机透明度
            sakura.style.opacity = Math.random() * 0.8 + 0.2;
            
            // 随机动画持续时间
            const duration = Math.random() * 10 + 10;
            sakura.style.animationDuration = `${duration}s`;
            
            // 随机旋转角度
            sakura.style.transform = `rotate(${Math.random() * 360}deg)`;
            
            sakuraContainer.appendChild(sakura);
            
            // 动画结束后移除元素
            setTimeout(() => {
                sakura.remove();
            }, duration * 1000);
        }
        
        // 定时创建樱花
        setInterval(createSakura, 300);
        
        // 页面加载时创建一批樱花
        window.onload = function() {
            for (let i = 0; i < 20; i++) {
                createSakura();
            }
            
            // 自动播放音乐
            const audio = document.getElementById('background-music');
            if (audio) {
                // 尝试自动播放，有些浏览器需要用户交互才能播放
                audio.play().catch(e => {
                    console.log('无法自动播放音乐，需要用户交互:', e);
                });
            }
        };
        
        // 点赞功能
        $(document).on('click', '.like-btn', function() {
            const $btn = $(this);
            const type = $btn.data('type');
            const id = $btn.data('id');
            const $icon = $btn.find('i');
            const $count = $btn.find('.like-count');
            
            // 检查是否已经点赞
            const isLiked = $icon.hasClass('liked');
            
            $.ajax({
                url: 'ajax.php',
                type: 'POST',
                data: {
                    action: isLiked ? 'unlike' : 'like',
                    type: type,
                    id: id
                },
                success: function(response) {
                    const data = JSON.parse(response);
                    if (data.success) {
                        if (isLiked) {
                            $icon.removeClass('liked');
                            $count.text(parseInt($count.text()) - 1);
                        } else {
                            $icon.addClass('liked');
                            $count.text(parseInt($count.text()) + 1);
                        }
                    } else {
                        alert(data.message);
                    }
                }
            });
        });
        
        // 评论显示/隐藏
        $(document).on('click', '.comment-toggle', function() {
            const type = $(this).data('type');
            const id = $(this).data('id');
            const $comments = $(`#comments-${type}-${id}`);
            
            if ($comments.is(':visible')) {
                $comments.hide();
            } else {
                $comments.show();
            }
        });
        
        // 提交评论
        $(document).on('click', '.comment-submit', function() {
            const $btn = $(this);
            const type = $btn.data('type');
            const id = $btn.data('id');
            const $input = $btn.prev('.comment-input');
            const content = $input.val().trim();
            
            if (!content) {
                alert('请输入评论内容');
                return;
            }
            
            $.ajax({
                url: 'ajax.php',
                type: 'POST',
                data: {
                    action: 'comment',
                    type: type,
                    id: id,
                    content: content
                },
                success: function(response) {
                    const data = JSON.parse(response);
                    if (data.success) {
                        // 清空输入框
                        $input.val('');
                        // 刷新评论区
                        const $comments = $(`#comments-${type}-${id}`);
                        $comments.load(window.location.href + ' ' + `#comments-${type}-${id} > *`);
                    } else {
                        alert(data.message);
                    }
                }
            });
        });
    </script>
</body>
</html>
